package com;

import com.mapper.PassengerMapper;
import com.pojo.Passenger;
import com.utils.MyBatisUtils;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author YDW
 * projectName:project2202_3
 * time:2022/4/28 15:57
 * description:
 */
public class testMybatis {
    @Test
    public void queryAll(){
        PassengerMapper mapper = MyBatisUtils.getMapper(PassengerMapper.class);
        List<Passenger> passengers = mapper.queryAll();
        for (Passenger passenger : passengers) {
            System.out.println(passenger);
            System.out.println(passenger.getPassport());
        }
    }
    @Test
    public void testByCondition(){
        PassengerMapper mapper = MyBatisUtils.getMapper(PassengerMapper.class);
        Map<String,Object> map=new HashMap<>();
        map.put("age",19);
        map.put("input","王");
        List<Passenger> passengers= mapper.queryByCondition(map);
        for (Passenger passenger : passengers) {
            System.out.print(passenger);
            System.out.println(passenger.getPassport());
        }
    }
    @Test
    public void testUpdate(){
        PassengerMapper mapper = MyBatisUtils.getMapper(PassengerMapper.class);
        Passenger passenger=new Passenger();
        passenger.setId(3);
        passenger.setAge("20");
        passenger.setName("小李");
        int i=mapper.update(passenger);
        MyBatisUtils.commit();
        System.out.println(i+"行受影响");
    }
    @Test
    public void testDelete(){
        PassengerMapper mapper = MyBatisUtils.getMapper(PassengerMapper.class);
        List<Integer> ids=new ArrayList<>();
        ids.add(4);
        ids.add(5);
        ids.add(6);
        ids.add(7);
        ids.add(8);
        int i =mapper.deleteByIds(ids);
        System.out.println(i+"行受到影响");
        MyBatisUtils.commit();
    }
    @Test
    public void inserts(){
        PassengerMapper mapper = MyBatisUtils.getMapper(PassengerMapper.class);
        List<Passenger> passengers=new ArrayList<Passenger>();
        Passenger p1=new Passenger(4,"四","18",null);
        Passenger p2=new Passenger(5,"五","19",null);
        Passenger p3=new Passenger(6,"六","20",null);
        Passenger p4=new Passenger(7,"七","18",null);
        Passenger p5=new Passenger(8,"八","18",null);
        passengers.add(p1);
        passengers.add(p2);
        passengers.add(p3);
        passengers.add(p4);
        passengers.add(p5);
        int i =mapper.inserts(passengers);
        System.out.println(i+"行受到影响");
        MyBatisUtils.commit();
    }
}
